﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XQ.Framework.String
{
    

    public class ChineseNumberConverter
    {
        private static readonly string[] Digits = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };

        public static string ConvertToChineseUpper(decimal number)
        {
            if (number < 0)
                throw new ArgumentException("Number must be non-negative.");

            if (number == 0)
                return "零元整";

            // 分离整数和小数部分
            long integerPart = (long)Math.Truncate(number);
            decimal fractionalPart = number - integerPart;

            // 处理小数部分（精确到分）
            int jiao = (int)(fractionalPart * 10) % 10;
            int fen = (int)(fractionalPart * 100) % 10;

            string integerStr = ConvertIntegerPart(integerPart);
            string fractionalStr = ConvertFractionalPart(jiao, fen);

            return integerStr + fractionalStr;
        }

        private static string ConvertIntegerPart(long integerPart)
        {
            if (integerPart == 0)
                return "零元";

            string numStr = integerPart.ToString().PadLeft(12, '0');
            string[] groups = {
            numStr.Substring(0, 4),  // 亿级
            numStr.Substring(4, 4),  // 万级
            numStr.Substring(8, 4)   // 个级
        };

            string[] results = new string[3];
            for (int i = 0; i < 3; i++)
                results[i] = ConvertFourDigitGroup(groups[i]);

            StringBuilder sb = new StringBuilder();
            string[] units = { "亿", "万", "" };

            for (int i = 0; i < 3; i++)
            {
                if (string.IsNullOrEmpty(results[i]))
                    continue;

                // 检查是否需要添加前导零
                if (sb.Length > 0 && groups[i][0] == '0')
                    sb.Append("零");

                sb.Append(results[i]).Append(units[i]);
            }

            // 处理组间缺失的零（如亿和个级之间）
            if (!string.IsNullOrEmpty(results[0]) &&
                string.IsNullOrEmpty(results[1]) &&
                !string.IsNullOrEmpty(results[2]))
            {
                if (groups[2][0] == '0' || sb.ToString().EndsWith("亿"))
                    sb.Append("零");
            }

            return sb.Append("元").ToString();
        }

        private static string ConvertFourDigitGroup(string group)
        {
            if (group.All(c => c == '0'))
                return string.Empty;

            char[] chars = group.ToCharArray();
            string[] units = { "仟", "佰", "拾", "" };
            StringBuilder sb = new StringBuilder();
            bool lastIsZero = true; // 标记上一位是否是零
            bool hasOutput = false;  // 标记是否已输出非零数字

            for (int i = 0; i < 4; i++)
            {
                int digit = chars[i] - '0';

                if (digit == 0)
                {
                    lastIsZero = true;
                }
                else
                {
                    if (hasOutput && lastIsZero)
                        sb.Append("零");

                    sb.Append(Digits[digit]).Append(units[i]);
                    hasOutput = true;
                    lastIsZero = false;
                }
            }

            return sb.ToString();
        }

        private static string ConvertFractionalPart(int jiao, int fen)
        {
            if (jiao == 0 && fen == 0)
                return "整";

            StringBuilder sb = new StringBuilder();

            if (jiao != 0)
            {
                sb.Append(Digits[jiao]).Append("角");
                if (fen == 0)
                    sb.Append("整");
                else
                    sb.Append(Digits[fen]).Append("分");
            }
            else if (fen != 0)
            {
                sb.Append("零").Append(Digits[fen]).Append("分");
            }

            return sb.ToString();
        }
    }
}
